<template>
	<view style="padding:30rpx;">
		<view class="u-bg-fff u-r-20" style="padding: 50rpx 20rpx;">
			<view class="u-flex-xy-center" style="flex-direction: column;padding: 50rpx 0;" @click="uploadImage">
				<u-avatar size="150rpx" :src='info.avatar'></u-avatar>
				<view class="u-f-30 u-bold u-m-t-20">修改头像</view>
			</view>
			<view class="u-flex-y-center u-flex-between" style="padding: 40rpx 0;border-bottom: 1px solid #f4f4f4;" @click="updateInfo('昵称','nickname')">
				<view class="u-f-28" style="color: #7C8191;">昵称</view>
				<view class="u-flex-y-center">
					<view class="u-f-28">{{info.nickname}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
			<view class="u-flex-y-center u-flex-between" style="padding: 40rpx 0;border-bottom: 1px solid #f4f4f4;">
				<view class="u-f-28" style="color: #7C8191;">手机号码</view>
				<view class="u-flex-y-center">
					<view class="u-f-28">{{info.mobile}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
			<view class="u-flex-y-center u-flex-between" style="padding: 40rpx 0;border-bottom: 1px solid #f4f4f4;"  @click="updateInfo('电子邮箱','email')">
				<view class="u-f-28" style="color: #7C8191;">电子邮箱</view>
				<view class="u-flex-y-center">
					<view class="u-f-28">{{info.email||''}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
			
			<view class="u-flex-y-center u-flex-between" style="padding: 40rpx 0;" @click='show2=true'>
				<view class="u-f-28" style="color:#7C8191;">性别</view>
				<view class="u-flex-y-center">
					<view class="u-f-28">{{info.gender==1?'男':'女'}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
			<view class="u-flex-y-center u-flex-between" style="padding: 40rpx 0;border-bottom: 1px solid #f4f4f4;" @click="updateInfo('出生日期','birthday')">
				<view class="u-f-28" style="color: #7C8191;">出生日期</view>
				<view class="u-flex-y-center">
					<view class="u-f-28">{{info.birthday||''}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
			<view class="u-flex-y-center u-flex-between" style="padding: 40rpx 0;border-bottom: 1px solid #f4f4f4;"  @click="updateInfo('我的地址','address')">
				<view class="u-f-28" style="color: #7C8191;">我的地址</view>
				<view class="u-flex-y-center">
					<view class="u-f-28">{{info.address||''}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
			<view class="u-flex-y-center u-flex-between" style="padding: 40rpx 0;border-bottom: 1px solid #f4f4f4;"  @click="updateInfo('所属俱乐部','job')">
				<view class="u-f-28" style="color: #7C8191;">所属俱乐部</view>
				<view class="u-flex-y-center">
					<view class="u-f-28">{{info.job||''}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
			<view class="u-flex-y-center u-flex-between" style="padding: 40rpx 0;border-bottom: 1px solid #f4f4f4;" @click="updateInfo('队伍名称','team_name')">
				<view class="u-f-28" style="color: #7C8191;">队伍名称</view>
				<view class="u-flex-y-center">
					<view class="u-f-28">{{info.team_name}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
			<view class="u-flex-y-center u-flex-between" style="padding: 40rpx 0;border-bottom: 1px solid #f4f4f4;" @click="updateInfo('队伍球衣','team_num')">
				<view class="u-f-28" style="color: #7C8191;">队伍球衣</view>
				<view class="u-flex-y-center">
					<view class="u-f-28">{{info.team_num}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
		</view>
		<u-gap height="60rpx"></u-gap>
		
		<view class="zhuxiao" @click="loginOut">退出登录</view>
		<u-overlay :show="show">
			<view style="width: 100vw;height: 100vh;" class="u-flex-xy-center">
				<view @click.stop class="card">
					<view class="u-p-30 u-m-b-26">
						<view class="u-text-center u-f-28 u-bold">修改{{update_title}}</view>
						<view class=" u-f-28 u-bold u-m-t-40">{{update_title}}</view>
						<u-gap height="30rpx"></u-gap>
						<u--input placeholder="请输入内容" border="surround" fontSize="28rpx" v-model="info[update_key]"></u--input>
					</view>
					<view class="u-flex-y-center u-f-28" style="border-top: 1px solid #F7F7F9;height: 100rpx;"
						>
						<view class="u-flex-xy-center"
							style="width: 50%;border-right: 1px solid #F7F7F9;height: 98rpx;" @click="show=false">
							取消
						</view>
						<view class="u-flex-xy-center" style="width: 50%;color: #209920;" @click="updateData(true)">
							确定
						</view>
					</view>
				</view>
			</view>
		</u-overlay>
		<u-popup :show="show2" round="30rpx" closeable @close="show2=false">
			<view class="u-p-30">
				<view class="u-f-32 u-bold u-text-center">选择性别</view>
				<view class="u-flex-y-center u-flex-around" style="padding: 60rpx 0;">
					<view class="u-flex-xy-center" style="flex-direction: column;" @click="info.gender=1">
						<image :src="info.gender==0?'../../static/nan.png':'../../static/naned.png'" style="width: 200rpx;height: 200rpx;"></image>
						<view class="u-f-28 u-m-t-12">男</view>
					</view>
					<view class="u-flex-xy-center" style="flex-direction: column;" @click="info.gender=0">
						<image :src="info.gender==1?'../../static/nv.png':'../../static/nved.png'" style="width: 200rpx;height: 200rpx;"></image>
						<view class="u-f-28 u-m-t-12">女</view>
					</view>
				</view>
				<u-gap height="100rpx"></u-gap>
				<view class="bottom u-flex-between" style="box-shadow: 0rpx 0rpx 16rpx 1rpx rgba(0,0,0,0.1);">
					<view class="btn" style="border-radius: 88rpx;width: 330rpx;margin: 0;" @click="show2=false">取消</view>
					<view class="btn1" style="border-radius: 88rpx;width: 330rpx;;margin: 0;" @click="updateData(false)">确认</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				show2: false,
					info:{},
					update_title:'',
					update_key:[]
					
			}
		},
		onLoad(e) {
					
					this.getUser()
				},
		methods: {
			zhuxiao(){
				uni.showModal({
					title: "注销账户",
					content: "请确认您的账户不再继续使用，账号内订单已完结，积分也将清零，注销后数据将无法恢复，请确认。",
					confirmText: "确认注销",
					cancelText: '取消',
					success: (r) => {
						if (r.confirm) {
							uni.showToast({
								title: "请联系客服为您处理账号注销",
								icon: 'none'
							})
						}
					}
				})
			},
			updateInfo(t,k){
				this.update_title = t
				this.update_key = k
				this.show = true
			},
			async updateData(valide = false){
				console.log(valide)
				if(valide&&!this.info[this.update_key]) {
						return uni.showToast({
						title: "内容不能为空",
						icon: 'none'
					})
				}
				let res= await uni.$u.http.post('user/profile',this.info);
				this.show = false
				this.show2 = false
				uni.showToast({
					title: "修改成功",
					icon: 'none'
				})
				this.getUser()
			},
			async uploadImage() {
				var _this = this
				uni.chooseImage({
					count: 1, //默认9
					success(chooseImageRes) {
						uni.showLoading({
							title: '上传中'
						})
						const tempFilePaths = chooseImageRes.tempFilePaths;
						for (var i in tempFilePaths) {
							uni.$u.http.upload('/common/upload', {
								/* 会加在url上 */
								filePath: tempFilePaths[i],
								name: 'file', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
								header: {
									token: uni.getStorageSync('token')
								}
							}).then(res => {
								_this.info.avatar = res.fullurl
								_this.updateData()
							}).catch(err => {
								uni.hideLoading()
							})
						}
						_this.$nextTick(function() {
							uni.hideLoading()
						})
					}
				})
			},
			async getUser(){
				let res= await uni.$u.http.post('user/index');
				this.info = res
				uni.setStorageSync('userInfo', res)
			},
			loginOut(){
				uni.removeStorageSync('token')
				uni.removeStorageSync('userInfo')
				uni.navigateTo({
					url:'/pages/login/login'
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #F8F8FA;
	}
	.zhuxiao {
		text-align: center;
	
		color: #EA4D44;
		font-size: 28rpx;
		
	}
	.bottom {
		padding: 10rpx 30rpx;
		bottom: 0;
		left: 0;
		position: fixed;
		width: 100%;
		height: 108rpx;
		background-color: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-radius: 30rpx 30rpx 0 0;
	
		
	
		.btn1 {
			width: 260rpx;
			height: 88rpx;
			background: linear-gradient(180deg, #209920 0%, #AAD777 100%);
			border-radius: 0rpx 44rpx 44rpx 0rpx;
			text-align: center;
			line-height: 88rpx;
			font-size: 28rpx;
			color: #fff;
		}
	}
	

	.card {
		width: 584rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
	}

	.btn {
		width: 100%;
		height: 88rpx;
		background: #F4F4F4;
		border-radius: 44rpx 44rpx 44rpx 44rpx;
		border: 1rpx solid #DFDFDF;
		text-align: center;
		line-height: 88rpx;
		font-size: 28rpx;
	}
</style>